import {
    validate,
    IsInt,
    IsPositive
} from 'class-validator'

import { Wallet } from '@app/wallet'
import { Tasks } from '@app/storage/task'
import * as utils from '@app/methods/utils'

class Request {
    @IsInt()
    @IsPositive()
    id: number = 0
}

/**
 * 获取任务状态
 */
export async function getTaskStatus(wallet: Wallet, params: any, callback: any) {
    const model = utils.makeModel(new Request, params)
    const errors = await validate(model)
    if (errors.length > 0) {
        callback(utils.BadRequest2(errors), undefined)
        return
    }

    const task = await Tasks.get(model.id)
    if (!task) {
        callback(undefined, null)
        return
    }
    callback(undefined, {id: task?.id, done: task?.done, count: task?.count})
}
